//
//  RegisterViewController.m
//  CCJD
//
//  Created by foreveross－bj on 14/12/20.
//  Copyright (c) 2014年 foreveross－bj. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterView.h"

#define kTopSpace   80
#define kLeftSpace  30
#define kIRSpace    30
#define kILong      35
#define kITopSpace  30

@interface RegisterViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;              //滑动视图
@property (nonatomic, strong) RegisterView *registerView;   //注册页面

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //添加背景图片
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"secondBackground"]];
    backgroundImageView.frame = self.view.frame;
    [self.view addSubview:backgroundImageView];
    //
    [self initView]; //初始化子视图
    
    //监控键盘
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    //添加点击事件 用于隐藏键盘
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenKeyboard:)];
    [self.view addGestureRecognizer:tapGesture];
}
/**
 * 初始化子视图
 **/
- (void)initView{
    //添加滑动视图
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scrollView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width,_scrollView.frame.size.height);
    [self.view addSubview:_scrollView];
    
    //添加注册视图
    self.registerView = [[RegisterView alloc] initWithFrame:CGRectMake(kLeftSpace, kTopSpace, self.view.frame.size.width - kLeftSpace * 2, 0) requestName:@"立即注册"];
    _registerView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
    //获取验证码操作
    _registerView.getCode = ^(){
        
    };
    //注册操作
    _registerView.doRegister = ^(NSString *phoneNumber, NSString *securityCode, NSString *password, BOOL isAgree){
        
    };
    [_scrollView addSubview:_registerView];
    
    //添加退出按钮
    UIImageView *exitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width - kIRSpace - kILong, kITopSpace, kILong, kILong)];
    exitImageView.image = [UIImage imageNamed:@"close"];
    UITapGestureRecognizer *exitTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(exit:)];
    [exitImageView addGestureRecognizer:exitTapGesture];
    exitImageView.userInteractionEnabled = YES;
    [_scrollView addSubview:exitImageView];
}


/**
 * 键盘显示调用此方法
 * @param notification
 **/
- (void)keyBoardWillShow:(NSNotification *)notification{
    
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
#endif
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_2
        NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
#else
        NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey];
#endif
        CGRect keyboardBounds;
        [keyboardBoundsValue getValue:&keyboardBounds];
        if (keyboardBounds.origin.y < _registerView.frame.origin.y + _registerView.frame.size.height) {
             CGFloat y =  _registerView.frame.origin.y + _registerView.frame.size.height - keyboardBounds.origin.y + 30;
            _scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height + y);
        }else{
            [self keyBoardWillHide:nil];
        }
    }
}

/**
 * 键盘将要隐藏时调用此方法
 * @param notification
 **/
- (void)keyBoardWillHide:(NSNotification *)notification{
    _scrollView.contentSize = CGSizeMake(self.view.frame.size.width,self.view.frame.size.height);
}
/**
 * 隐藏键盘
 * @param tapGesture
 **/
- (void)hiddenKeyboard:(UITapGestureRecognizer *)tapGesture{
    [_registerView.passwordTextField resignFirstResponder];
    [_registerView.phoneTextField resignFirstResponder];
    [_registerView.securityCodeTextField resignFirstResponder];
}

/**
 * 点击退出按钮后执行此方法实现退出工能
 * @param tapGesture
 **/
- (void)exit:(UITapGestureRecognizer *)tapGesture{
    [self dismissViewControllerAnimated:YES completion:nil];
}

/**
 * 改变状态栏前景色
 * @return UIStatusBarStyle
 **/
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
